#!/bin/sh
#*********************************************************************
#   Copyright (c) International Business Machines  Corp., 2000
#
#   This program is free software;  you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY;  without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
#   the GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program;  if not, write to the Free Software
#   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
#  FILE   : ssh
#
#  PURPOSE: Tests to start stop ssh service.
#
#  SETUP: The program `/usr/bin/expect' MUST be installed.
#
#  HISTORY:
#    03/03 Jerone Young (jeroney@us.ibm.com)
#
#  Filename:    ssh04
#  Version:     1.0
#  Date:        2013-5-13
#  Author:      Heihuijing
#  Email:       huijing.hei@cs2c.com.cn
#  Summary:     02系统基本功能-04网络服务-10SSH-01更改服务状态-01启动ssh服务
#               02系统基本功能-04网络服务-10SSH-01更改服务状态-02停止ssh服务
#  History:
#               Version 1.0, 2013-5-13
#               - can start and stop sshd service on localhost.

#-----------------------------------------------------------------------
# FUNCTION:  do_setup
#-----------------------------------------------------------------------

do_setup()
{

    RHOST="localhost"

    #erase user if he may exist , so we can have a clean env
    exists expect ssh 

}

#-----------------------------------------------------------------------
# FUNCTION:  do_test
#-----------------------------------------------------------------------

function do_test()
{
    NUMLOOPS=${NUMLOOPS:-4}
    SLEEPTIME=${SLEEPTIME:-3}
    TST_COUNT=${TST_COUNT:-1}

    service sshd status | grep pid > /dev/null 2>&1
    if [ $? -eq 0 ];then
        EchoInfo "ssh04   :  before testing, service sshd is running..."
    else
        EchoInfo "ssh04   :  before testing, service sshd is stopped."
    fi


    while [ $TST_COUNT -le $NUMLOOPS ]; do 

        service sshd status | grep pid > /dev/null 2>&1
        if [ $? -ne 0 ];then
            service sshd start > /dev/null 2>&1
            sleep $SLEEPTIME
            service sshd status | grep pid > /dev/null 2>&1
            EchoResult "ssh04   :  service sshd start."
        else
            service sshd stop > /dev/null 2>&1
            sleep $SLEEPTIME
            ! service sshd status | grep pid > /dev/null 2>&1
            EchoResult "ssh04   :  service sshd stop."
        fi

        TST_COUNT=$((TST_COUNT+1))
    done
  return 0

}

#-----------------------------------------------------------------------
# FUNCTION:  do_cleanup
#-----------------------------------------------------------------------

do_cleanup(){
    /etc/init.d/sshd restart
    EchoInfo "restart sshd service."
}

#-----------------------------------------------------------------------
# FUNCTION:  MAIN
#
# DESCRIPTION: Create Test User
#          Call upon script to make sure test user cannont log in with invalid password
#          Cleanup Test User from system
#          Exit with exit code of script called upon
#-----------------------------------------------------------------------
if [ -z "$SFROOT" ]
then
    CMDLIB=./
    SFROOT=./
else
    CMDLIB=${SFROOT}/testcases/Network_Service
fi

. ${SFROOT}/lib/Echo.sh
. ${CMDLIB}/lib/net_cmdlib.sh


read_opts $*
do_setup
do_test
do_cleanup
exit 0
